package app.fugema.shop.main.center.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.base.common.app.LoginUtils;
import com.base.common.netBeanPackage.LiveDataWrapper;
import com.base.common.utils.TimeUtil;
import com.base.common.utils.UIUtils;
import com.base.common.view.base.BaseFragment;
import com.base.common.view.base.BaseTitleActivity;
import com.base.common.viewmodel.BaseViewObserver;
import app.fugema.shop.databinding.GroupBookingListAcBinding;

import app.fugema.shop.R;
import app.fugema.shop.main.center.model.CouponListResult;
import app.fugema.shop.main.center.vm.UserVM;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;

public class CouponListActivity extends BaseTitleActivity<GroupBookingListAcBinding, UserVM>
{
    /**
     * @param context
     *
     */
    public static void start(Context context)
    {
        Intent intent = new Intent(context, CouponListActivity.class);
        context.startActivity(intent);
    }


    private List<BaseFragment> fragmentList = new ArrayList<>();
    private List<String> titleList = new ArrayList<>();

    @Override
    protected int getStatusBarColor()
    {
        return R.color.redColor;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.group_booking_list_ac);
    }


    @Override
    protected void initView()
    {
        super.initView();
        setCloseImg(R.mipmap.ic_back_white);
        setTitle("我的优惠券",R.color.white);

        //if (source < Item.values().length)
        {
            //binding.vpPager.setCurrentItem(source, false);
        }
    }


    @Override
    protected void initData()
    {
        super.initData();
        String userKey = LoginUtils.getToken();
        viewModel.getUserCounponList(userKey)
                 .observe(this, new BaseViewObserver<LiveDataWrapper<CouponListResult>>()
                 {
                     @Override
                     protected void onSuccess(LiveDataWrapper<CouponListResult> data)
                     {
                        //查询 优惠券
                         setViewData( data.data );
                     }

                     @Override
                     public boolean isEmptyData(LiveDataWrapper<CouponListResult> data)
                     {
                         return  UIUtils.isEmpty( data.data.getCoupon() );
                     }
                 });
    }

    private void setViewData(CouponListResult couponResult)
    {
        titleList.add("可用优惠券("+couponResult.getNoUseCount()+")");
        titleList.add("不可用优惠券("+couponResult.getUserCount()+")");

        List<CouponListResult.CouponBean> canUseList = new ArrayList<>();
        List<CouponListResult.CouponBean> overUseList = new ArrayList<>();

        Long nowTime = System.currentTimeMillis();

        for (CouponListResult.CouponBean couponBean : couponResult.getCoupon())
        {
            // 当前时间 大于等于 优惠券结束时间   为过期优惠券   2021/5/22 0:00:00
            //0=未使用，1=已使用  UseStatus

            if( nowTime >= TimeUtil.getMilliTimeByDateStr(couponBean.getEndTime(),"yyyy/MM/dd HH:mm:ss") || couponBean.getUseStatus() == 1 )
            {
                overUseList.add( couponBean );
            }
            else
            {
                canUseList.add( couponBean );
            }
        }

        fragmentList.add(CouponListFragment.getInstance( canUseList ) );
        fragmentList.add(CouponListFragment.getInstance( overUseList ) );

        binding.vpPager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position)
            {
                return fragmentList.get(position);
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position)
            {
                return titleList.get(position);
            }

            @Override
            public int getCount()
            {
                return fragmentList.size();
            }
        });
        viewUtils.initSlidingTabLayout(binding.stlTab, binding.vpPager);
    }
}
